
/**
 * @Project : Clion_ProgrammingExercises
 * @File : 2
 * @Author : WangRuoyu 
 * @Date : 2023/5/16 15:16
 */

#include "iostream"

using namespace std;

const int N = 2010;

struct time {
    int start;
    int over;
} a[N], b[N];

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i].start;
        cin >> a[i].over;
    }
    for (int i = 0; i < n; ++i) {
        cin >> b[i].start;
        cin >> b[i].over;
    }

    int x = 0;
    int y = 0;
    int res = 0;

    while (x < n && y < n) {
        res += max(0, min(a[x].over, b[y].over) - max(a[x].start, b[y].start));
        if (a[x].over < b[y].over) {
            x++;
        } else if (a[x].over > b[y].over) {
            y++;
        } else {
            x++;
            y++;
        }
    }

    cout << res;

    return 0;
}